package cn.net.csit.platform_cloud.basicmodel.controller;

import cn.net.csit.platform_cloud.basicmodel.domain.BasicPropertyDictionaryDomain;
import cn.net.csit.platform_cloud.basicmodel.service.IBasicObjectPropertyRelationService;
import cn.net.csit.platform_cloud.basicmodel.service.IBasicPropertyDictionaryService;
import cn.net.csit.platform_cloud.utils.BaseResult;
import cn.net.csit.platform_cloud.utils.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * 属性字典管理
 *
 * @ClassName:BasicPropertyDictionaryController
 *
 * @Author:yanwenjuan
 *
 * @Date:2019年4月2日
 *
 * @Description:属性字典管理
 *
 *
 */


@RestController
@RequestMapping(value = "/propDict")
public class BasicPropertyDictionaryController {
    @Autowired
    IBasicPropertyDictionaryService basicPropertyDictionaryService;
    @Autowired
    IBasicObjectPropertyRelationService basicObjectPropertyRelationService;
    //传递除了主键参数之外的其他属性，新增记录
    @PostMapping("/insert")
    public BaseResult insert(@RequestBody BasicPropertyDictionaryDomain recode){
        int i = basicPropertyDictionaryService.insert(recode);
        //更新属性组-属性关系
        basicPropertyDictionaryService.updatePropGroupRelation(recode);

        return ResultUtil.success(i);
    }
    //传递主键参数查询记录
    @PostMapping("/getByPK")
    public BaseResult getByPK(String uuid){
        BasicPropertyDictionaryDomain model=basicPropertyDictionaryService.selectById(uuid);
        return ResultUtil.success(model);
    }
    //传递主键参数及其他属性修改记录
    @PostMapping("/updateByPrimaryKey")
    public BaseResult updateByPrimaryKey(@RequestBody BasicPropertyDictionaryDomain recode){
        int i = basicPropertyDictionaryService.updateByPrimaryKey(recode);
        //更新属性组-属性关系
        basicPropertyDictionaryService.updatePropGroupRelation(recode);

        return ResultUtil.success(i);
    }
    //传递主键参数删除记录
    @PostMapping("/deleteByPrimaryKey")
    public BaseResult deleteByPrimaryKey(String uuid, String propGroupName){
        int i = 0;
        if("" != uuid){
            i = basicPropertyDictionaryService.deleteByPrimaryKey(uuid);
        }
        if("" != propGroupName){
            i = basicPropertyDictionaryService.deleteByPropGroupName(propGroupName);
        }
        return ResultUtil.success(i);
    }

    //查询所有记录
    @GetMapping("/getAll")
    public BaseResult getAll(){
        List<BasicPropertyDictionaryDomain> list=basicPropertyDictionaryService.getAll();
        PageInfo<BasicPropertyDictionaryDomain> info=new PageInfo<>(list);
        return ResultUtil.success(info);
    }

    //6.查询所有记录--条件查询分页
    @PostMapping("/getAllChooseByPage")
    public BaseResult getAllChooseByPage(Pageable pageable,String propGroupName, String propName){
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,String> map=new HashMap<>();
        if(null!=propGroupName && ""!=propGroupName) {
            map.put("propGroupName", propGroupName);
        }
        if(null!=propName && ""!=propName){
            map.put("propName", propName);
        }
        list=basicPropertyDictionaryService.getAllByGroup(map);

        PageInfo<Map<String,Object>> info=new PageInfo<>(list);
        return ResultUtil.success(info);
    }
    //8.根据属性uuid，批量修改属性组字段--新建，删除属性组时使用
    @PostMapping("/updateCloumnByPrimaryKey")
    public BaseResult updateCloumnByPrimaryKey(String uuid,String params){
        int i = basicPropertyDictionaryService.updateByPrimaryKey(uuid,params);
        return ResultUtil.success(i);
    }

    //9.根据属性组名称，批量修改属性组名称字段--修改属性组时使用
    @PostMapping("/updateByGroupNameList")
    public BaseResult updateByGroupNameList(String oldGroupName,String newGroupName){
        int i = basicPropertyDictionaryService.updateByGroupNameList(oldGroupName,newGroupName);
        return ResultUtil.success(i);
    }
}
